﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Rich4Editor
{
    class FileEditor
    {
        private String     file;         // 读写的文件
        private Byte3Var[] currencyArray;// 现金
        private Byte3Var[] depositArray; // 存款
        private Byte2Var[] dotArray;     // 点券
        private Daoju[]    daojuArray;   // 道具
        private Card[]     cardArray;    // 卡片

        public void read(String inFile)
        {            

            file = inFile;

            FileStream fs = new FileStream(file,FileMode.Open);
            currencyArray = new Byte3Var[4];
            currencyArray[0] = new Byte3Var("玩家1现金", "2c", fs);
            currencyArray[1] = new Byte3Var("玩家2现金", "94", fs);
            currencyArray[2] = new Byte3Var("玩家3现金", "fc", fs);
            currencyArray[3] = new Byte3Var("玩家4现金", "164", fs);
            System.Console.WriteLine("现金读取完毕");
            
            depositArray = new Byte3Var[4];
            depositArray[0] = new Byte3Var("玩家1存款", "30", fs);
            depositArray[1] = new Byte3Var("玩家2存款", "98", fs);
            depositArray[2] = new Byte3Var("玩家3存款", "100", fs);
            depositArray[3] = new Byte3Var("玩家4存款", "168", fs);
            System.Console.WriteLine("存款读取完毕");

            dotArray = new Byte2Var[4];
            dotArray[0] = new Byte2Var("玩家1点券", "40", fs);
            dotArray[1] = new Byte2Var("玩家2点券", "a8", fs);
            dotArray[2] = new Byte2Var("玩家3点券", "110", fs);
            dotArray[3] = new Byte2Var("玩家4点券", "178", fs);
            System.Console.WriteLine("点券读取完毕");

            daojuArray = new Daoju[4];
            daojuArray[0] = new Daoju("玩家1道具", "690", fs);
            daojuArray[1] = new Daoju("玩家2道具", "69f", fs);
            daojuArray[2] = new Daoju("玩家3道具", "6ae", fs);
            daojuArray[3] = new Daoju("玩家4道具", "6bd", fs);
            System.Console.WriteLine("道具读取完毕");

            cardArray = new Card[4];
            cardArray[0] = new Card("玩家1卡片", "654", fs);
            cardArray[1] = new Card("玩家2卡片", "663", fs);
            cardArray[2] = new Card("玩家3卡片", "672", fs);
            cardArray[3] = new Card("玩家4卡片", "681", fs);
            System.Console.WriteLine("卡片读取完毕");

            fs.Close();
            System.Console.WriteLine("全部数据读取完毕");
        }

        public int[] getCurrencyArray()
        {
            int[] arr = new int[4];

            arr[0] = currencyArray[0].Point;
            arr[1] = currencyArray[1].Point;
            arr[2] = currencyArray[2].Point;
            arr[3] = currencyArray[3].Point;

            return arr;
        }

        public void setCurrencyArray(int[] arr)
        {
            for (int i = 0; i < arr.Length; i++)
            {
                currencyArray[i].Point = arr[i];
            }
        }

        public void saveCurrencyArray(FileStream writeStream)
        {
            for (int i = 0; i < currencyArray.Length; i++)
            {
                currencyArray[i].write(writeStream);
            }
        }

        public int[] getDepositArray()
        {
            int[] arr = new int[4];

            arr[0] = depositArray[0].Point;
            arr[1] = depositArray[1].Point;
            arr[2] = depositArray[2].Point;
            arr[3] = depositArray[3].Point;

            return arr;
        }

        public void setDepositArray(int[] arr)
        {
            for (int i = 0; i < arr.Length; i++)
            {
                depositArray[i].Point = arr[i];
            }
        }

        public void saveDepositArray(FileStream writeStream)
        {
            for (int i = 0; i < currencyArray.Length; i++)
            {
                depositArray[i].write(writeStream);
            }
        }


        public int[] getDotArray()
        {
            int[] arr = new int[4];

            arr[0] = dotArray[0].Point;
            arr[1] = dotArray[1].Point;
            arr[2] = dotArray[2].Point;
            arr[3] = dotArray[3].Point;

            return arr;
        }

        public void setDotArray(int[] arr)
        {
            for (int i = 0; i < arr.Length; i++)
            {
                dotArray[i].Point = arr[i];
            }
        }

        public void saveDotArray(FileStream writeStream)
        {
            for (int i = 0; i < currencyArray.Length; i++)
            {
                dotArray[i].write(writeStream);
            }
        }

        public Daoju[] getDaojuArray()
        {
            return daojuArray;
        }

        public Card[] getCardArray()
        {
            return cardArray;
        }

        public void setDaoju(int[] arr0,int[] arr1,int[] arr2,int[] arr3)
        {

            daojuArray[0].setPoints(arr0);
            daojuArray[1].setPoints(arr1);
            daojuArray[2].setPoints(arr2);
            daojuArray[3].setPoints(arr3);
        }

        public void setCard(String[] arr0, String[] arr1, String[] arr2, String[] arr3)
        {

            cardArray[0].setCards(arr0);
            cardArray[1].setCards(arr1);
            cardArray[2].setCards(arr2);
            cardArray[3].setCards(arr3);
        }

        public void saveDaoju(FileStream writeStream)
        {
            for (int i = 0; i < currencyArray.Length; i++)
            {
                daojuArray[i].write(writeStream);
            }
        }

        public void saveCard(FileStream writeStream)
        {
            for (int i = 0; i < cardArray.Length; i++)
            {
                cardArray[i].write(writeStream);
            }
        }
    }

    public class Byte3Var
    {
        private String name;
        private String pos;
        private int nPos;
        private byte[] data;
        private int point;

        public int Point
        {
            get { return point; }
            set { point = value; }
        }


        public Byte3Var(String name, String pos, FileStream fs)
        {
            this.name = name;
            this.pos = pos;
            this.nPos = Int32.Parse(this.pos, System.Globalization.NumberStyles.HexNumber);
            System.Console.WriteLine(name + "位置=" + nPos);
            data = new byte[3];

            read(fs);
        }

        public void read(FileStream fs)
        {
            fs.Seek(this.nPos, SeekOrigin.Begin);
            fs.Read(this.data, 0, this.data.Length);

            int a = data[2];
            String strA = Convert.ToString(a, 16);
            if(strA.Equals("")){
                strA="00";
            }
            if (strA.Length==1)
            {
                strA = "0" + strA;
            }

            a = data[1];
            String strB = Convert.ToString(a, 16);
            if (strB.Equals(""))
            {
                strB = "00";
            }
            if (strB.Length == 1)
            {
                strB = "0" + strB;
            }


            a = data[0];
            String strC = Convert.ToString(a, 16);
            if (strC.Equals(""))
            {
                strC = "00";
            }
            if (strC.Length == 1)
            {
                strC = "0" + strC;
            }


            String strD = strA + strB + strC;

            point = Convert.ToInt32(strD, 16);

            System.Console.WriteLine(name+"="+point);
        }

        public void write(FileStream writeStream)
        {
            int value = this.point;// 要写入的数值
            String strAll = value.ToString("x8");// 转成16进制

            // 下面是特定处理，个性化需求，不完善勿怪
            String strA = strAll.Substring(strAll.Length - 2, 2);
            String strB = strAll.Substring(strAll.Length - 4, 2);
            String strC = strAll.Substring(strAll.Length - 6, 2);

            byte[] newData = new byte[3];
            newData[0] = Convert.ToByte(strA, 16);
            newData[1] = Convert.ToByte(strB, 16);
            newData[2] = Convert.ToByte(strC, 16);

            writeStream.Seek(this.nPos, SeekOrigin.Begin);// 定位
            writeStream.Write(newData, 0, newData.Length);// 将准备好的数组写入文件
           
        }

    }

    public class Byte2Var
    {
        private String name;
        private String pos;
        private int nPos;
        private byte[] data;
        private int point;

        public int Point
        {
            get { return point; }
            set { point = value; }
        }


        public Byte2Var(String name, String pos, FileStream fs)
        {
            this.name = name;
            this.pos = pos;
            this.nPos = Int32.Parse(this.pos, System.Globalization.NumberStyles.HexNumber);
            System.Console.WriteLine(name + "位置=" + nPos);
            data = new byte[2];

            read(fs);
        }

        public void read(FileStream fs)
        {
            fs.Seek(this.nPos, SeekOrigin.Begin);
            fs.Read(this.data, 0, this.data.Length);


            int a = data[1];
            String strB = Convert.ToString(a, 16);
            if (strB.Equals(""))
            {
                strB = "00";
            }
            if (strB.Length == 1)
            {
                strB = "0" + strB;
            }


            a = data[0];
            String strC = Convert.ToString(a, 16);
            if (strC.Equals(""))
            {
                strC = "00";
            }
            if (strC.Length == 1)
            {
                strC = "0" + strC;
            }


            String strD =  strB + strC;

            point = Convert.ToInt32(strD, 16);

            System.Console.WriteLine(name + "=" + point);
        }

        public void write(FileStream writeStream)
        {
            int value = this.point;// 要写入的数值
            String strAll = value.ToString("x8");// 转成16进制

            // 下面是特定处理，个性化需求，不完善勿怪
            String strA = strAll.Substring(strAll.Length - 2, 2);
            String strB = strAll.Substring(strAll.Length - 4, 2);
            // String strC = strAll.Substring(strAll.Length - 6, 2);

            byte[] newData = new byte[2];
            newData[0] = Convert.ToByte(strA, 16);
            newData[1] = Convert.ToByte(strB, 16);
            //newData[2] = Convert.ToByte(strC, 16);

            writeStream.Seek(this.nPos, SeekOrigin.Begin);// 定位
            writeStream.Write(newData, 0, newData.Length);// 将准备好的数组写入文件

        }

    }

    /// <summary>
    /// 道具类
    /// </summary>
    public class Daoju
    {
        private String name;
        private String pos;
        private int nPos;
        private byte[] data;
        private int[] points;

        public Daoju(String name, String pos, FileStream fs)
        {
            this.name = name;
            this.pos = pos;
            this.nPos = Int32.Parse(this.pos, System.Globalization.NumberStyles.HexNumber);
            System.Console.WriteLine(name + "位置=" + nPos);
            data = new byte[13];

            read(fs);
        }

        public void read(FileStream fs)
        {
            fs.Seek(this.nPos, SeekOrigin.Begin);
            fs.Read(this.data, 0, this.data.Length);


            String[] names = { "机器娃娃", "路障", "地雷", "定时炸弹", "机车", "汽车", "子弹", "遥控筛子", "机器工人", "时光机", "传送机", "工程车", "核子飞弹"};
            points = new int[13];

            for (int i = 0; i < data.Length;i++ )
            {

                int a = data[i];
                String strC = Convert.ToString(a, 16);
                if (strC.Equals(""))
                {
                    strC = "00";
                }
                if (strC.Length == 1)
                {
                    strC = "0" + strC;
                }

                points[i] = Convert.ToInt32(strC, 16);
                System.Console.WriteLine(names[i] + "=" + points[i]+"个");
            }
        }

        public void write(FileStream writeStream)
        {
            byte[] newData = new byte[13];

            for (int i = 0; i < newData.Length;i++ )
            {
                String str = Convert.ToString(points[i],16);

                newData[i] = Convert.ToByte(str,16);
            }


            writeStream.Seek(this.nPos, SeekOrigin.Begin);// 定位
            writeStream.Write(newData, 0, newData.Length);// 将准备好的数组写入文件

        }

        public int[] getPoints()
        {
            return points;
        }

        public void setPoints(int[] newArr)
        {
            points = newArr;
        }
    }

    public class Card
    {
        private String name;
        private String pos;
        private int nPos;
        private byte[] data;
        private String[] cards;

        private String[,] nameByteArray = {   { "01","均富卡"}, 
                                              { "02","均贫卡"}, 
                                              { "03","购地卡"}, 
                                              { "04","换地卡"}, 
                                              { "05","换屋卡"}, 
                                              { "06","转向卡"}, 
                                              { "07","改建卡"}, 
                                              { "08","拍卖卡"}, 
                                              { "09","天使卡"}, 
                                              { "0a","恶魔卡"}, 
                                              { "0b","怪兽卡"}, 
                                              { "0c","拆除卡"}, 
                                              { "0d","抢夺卡"}, 
                                              { "0e","停留卡"}, 
                                              { "0f","冬眠卡"}, 
                                              { "10","梦游卡"}, 
                                              { "11","陷害卡"}, 
                                              { "12","复仇卡"}, 
                                              { "13","嫁祸卡"}, 
                                              { "14","免费卡"}, 
                                              { "15","免罪卡"}, 
                                              { "16","送神符"}, 
                                              { "17","请神符"}, 
                                              { "18","红卡"}, 
                                              { "19","黑卡"}, 
                                              { "1a","查税卡"}, 
                                              { "1b","涨价卡"}, 
                                              { "1c","查封卡"}, 
                                              { "1d","同盟卡"}, 
                                              { "1e","乌龟卡"}, 
                              };

        public Card(String name, String pos, FileStream fs)
        {
            this.name = name;
            this.pos = pos;
            this.nPos = Int32.Parse(this.pos, System.Globalization.NumberStyles.HexNumber);
            System.Console.WriteLine(name + "位置=" + nPos);
            data = new byte[15];

            read(fs);
        }

        public void read(FileStream fs)
        {
            fs.Seek(this.nPos, SeekOrigin.Begin);
            fs.Read(this.data, 0, this.data.Length);

            cards = new String[15];

            for (int i = 0; i < data.Length; i++)
            {

                int a = data[i];
                String strC = Convert.ToString(a, 16);
                if (strC.Equals(""))
                {
                    strC = "00";
                }
                if (strC.Length == 1)
                {
                    strC = "0" + strC;
                }

                cards[i] = strC;               
            }

            System.Console.Write("卡片：" );
            for (int i = 0; i < cards.Length; i++)
            {
                System.Console.Write(getCardName(cards[i])+",");
            }
            System.Console.WriteLine("");
        }

        private String getByte(String name)
        {
            for (int i = 0; i < 30; i++)
            {
                if (name.Equals(nameByteArray[i,1]))
                {
                    return nameByteArray[i, 0];
                }
            }

            return "00";
        }

        private String getCardName(String cardByte)
        {

            for (int i = 0; i < 30; i++)
            {
                if (cardByte.Equals(nameByteArray[i, 0]))
                {
                    return nameByteArray[i, 1];
                }
            }

            return "";
        }

        /// <summary>
        /// 取得卡片名字的数组
        /// </summary>
        /// <returns></returns>
        public String[] getCardNameArray()
        {
            String[] retval = new String[15];

            for (int i = 0; i < cards.Length; i++)
            {
                retval[i]=getCardName(cards[i]) ;
            }

            return retval;
        }

        public void setCards(String[] arr)
        {
            for (int i = 0; i < cards.Length;i++ )
            {
                cards[i] = arr[i];
            }
        }


        public void write(FileStream writeStream)
        {
            byte[] newData = new byte[15];

            for (int i = 0; i < newData.Length; i++)
            {
                String str = getByte(cards[i]);

                newData[i] = Convert.ToByte(str, 16);
            }


            writeStream.Seek(this.nPos, SeekOrigin.Begin);// 定位
            writeStream.Write(newData, 0, newData.Length);// 将准备好的数组写入文件
        }
    }
}
